#define _CRT_SECURE_NO_WARNINGS 1
#include"Heap.h"
#include<time.h>

void test()
{
	Heap hp;
	HeapInit(&hp);
	int arr[] = { 9,1,2,3,6,3,5,7,4,5,6,34,24 };
	Heapsort(arr, sizeof(arr) / sizeof(int));
	for (int i = 0; i < sizeof(arr) / sizeof(int); i++)
	{
		printf("%d ", arr[i]);
	}
	/*while (!HeapEmpty(&hp))
	{
		Heapsort(arr,sizeof(arr)/sizeof(int));
		printf("%d",HeapTop(&hp));
	}*/
	HeapDestory(&hp);
}

void Numcreate()
{
	srand((unsigned int)time(0));
	int n = 100000;
	FILE* f = fopen("data.txt", "w");
	if (f == NULL)
	{
		perror("fopen fail");

		return ;
	}
	for (int i = 0; i < n; i++)
	{
		int x = rand() + i;
		fprintf(f,"%d\n", x);
	}
	fclose(f);
}

void Popk()
{
	FILE* pf = fopen("data.txt", "r");
	if (pf == NULL)
	{
		perror("fopen r fail");
	}
	int k = 0;
	printf("请输入k的值:<");
	scanf("%d", &k);
	int* arr = (int*)malloc(k*sizeof(int));
	if (arr == NULL)
	{
		perror("malloc fail");

		return;
	}
	for (int i = 0; i < k; i++)
	{
		fscanf(pf,"%d", &arr[i]);
	}
	for (int i = (k-1-1)/2; i >= 0; i--)
	{
		AdjustDown(arr, k,i);
	}
	int x = 0;
	while (fscanf(pf,"%d", & x) > 0)
	{
		if (x > arr[0])
		{
			arr[0] = x;
			AdjustDown(arr, k, 0);
		}
	}
	for (int i = 0; i < k; i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
}

void test1()
{
	int arr[5] = { 5,3,4,2,1 };
	AdjustDown(arr, 5, 0);
}

int main()
{

	//Numcreate();
	test();
	//Popk();
	//test1();
	return 0;
}